package com.zq.it.dao.impl.exam;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zq.it.dao.exam.TCourseWareDAO;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.exam.TCourseWare;

/**
 * TCourseWareDAO的实现类
 *
 * @author 王凡
 *
 * @version $ID: TCourseWareDAOImpl.java 2013-3-21 王凡@$
 */
public class TCourseWareDAOImpl extends BaseDao<TCourseWare> implements TCourseWareDAO {

	private static final Logger log = LoggerFactory
			.getLogger(TCourseWareDAOImpl.class);

	// property constants
	public static final String FILE_NAME = "fileName";
	public static final String NAME = "name";
	public static final String _TID = "TId";
	public static final String TYPE = "type";
	public static final String EXTEND_FIELD = "extendField";

	@Override
	public void save(TCourseWare transientInstance) {
		log.debug("saving TCourseWare instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(TCourseWare persistentInstance) {
		log.debug("deleting TCourseWare instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public TCourseWare findById(Long id) {
		log.debug("getting TCourseWare instance with id: " + id);
		try {
			TCourseWare instance = (TCourseWare) getHibernateTemplate()
					.get("com.zq.it.pojo.exam.TCourseWare", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(TCourseWare instance) {
		log.debug("finding TCourseWare instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TCourseWare instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TCourseWare as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByFileName(Object fileName) {
		return findByProperty(FILE_NAME, fileName);
	}

	@Override
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	@Override
	public List findByTId(Object TId) {
		return findByProperty(_TID, TId);
	}

	@Override
	public List findByType(Object fileName) {
		return findByProperty(TYPE, fileName);
	}

	@Override
	public List findByExtendField(Object extendField) {
		return findByProperty(EXTEND_FIELD, extendField);
	}

	@Override
	public List findAll() {
		log.debug("finding all TCourseWare instances");
		try {
			String queryString = "from TCourseWare";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public void update(TCourseWare instance) {
		log.debug("attaching dirty TCourseWare instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public List findByHql(String hql) {
		return getHibernateTemplate().find(hql);
	}
}